package cn.hx.admin.controller;

import cn.hx.admin.entity.UserSign;
import cn.hx.admin.service.UserSignService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (UserSign)表控制层
 *
 * @author makejava
 * @since 2023-12-11 10:55:29
 */
@RestController
@RequestMapping("userSign")
public class UserSignController {
    /**
     * 服务对象
     */
    @Resource
    private UserSignService userSignService;

    /**
     * 分页查询
     *
     * @param userSign 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<List<UserSign>> query(UserSign userSign) {
        return ResponseEntity.ok(this.userSignService.query(userSign));
    }

    /**
     * 分页查询
     *
     * @param userSign 筛选条件
     * @return 查询结果
     */
    @GetMapping("/userSignToday")
    public ResponseEntity<List<UserSign>> queryByToday(UserSign userSign) {

        // 获取当前日期
        Date currentDate = new Date();

// 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// 设置开始时间为当天0点0分0秒
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        userSign.setStartTime(sdf.format(cal.getTime()));

// 设置结束时间为当天23点59分59秒
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        userSign.setEndTime(sdf.format(cal.getTime()));
        System.out.println(userSign.getStartTime());
        System.out.println(userSign.getEndTime());
        return ResponseEntity.ok(this.userSignService.queryByToday(userSign));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<UserSign> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userSignService.queryById(id));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param deviceId 主键
     * @return 单条数据
     */
    @GetMapping("{deviceId}")
    public ResponseEntity<UserSign> queryByDeviceId(@PathVariable("deviceId") String deviceId) {
        return ResponseEntity.ok(this.userSignService.queryByDeviceId(deviceId));
    }

    /**
     * 新增数据
     *
     * @param userSign 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserSign> add(UserSign userSign) {
        return ResponseEntity.ok(this.userSignService.insert(userSign));
    }
    @GetMapping("/toUserAndDept")
    public ResponseEntity<List<Map>> queryByUserAndDept(Map map) {
        return ResponseEntity.ok(this.userSignService.queryByUserAndDept(map));
    }

    /**
     * 编辑数据
     *
     * @param userSign 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<UserSign> edit(UserSign userSign) {
        return ResponseEntity.ok(this.userSignService.update(userSign));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userSignService.deleteById(id));
    }

}

